package com.java.advance.simple.threadpool;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 使用多线程处理一批数据
 * Created by yangminghua on 2019/2/22.
 */
public class PoolDemo {


    public static void main(String[] args) {
//        new PoolDemo().func();
        new PoolDemo().dealListWithMutiThread();
    }

    public void dealListWithMutiThread() {
        List<String> list = new ArrayList<String>();
        int a = 0;
        while (a < 100) {
            list.add(a + "-->");
            a++;
        }
        int index = 0;
        ExecutorService ex = Executors.newFixedThreadPool(10);
        int dealSize = 20;
        List<Future<List<String>>> futures = new ArrayList<>();
        //分配
        int i = 0;
        while (i < 5) {
            System.out.println("加入批次：" + (i + 1));
            int start = index;
            if (start >= list.size()) break;
            int end = start + dealSize;
            end = end > list.size() ? list.size() : end;
            Task task = new Task(list, start, end);
            Future<List<String>> future = ex.submit(task);
            futures.add(future);
            index += dealSize;
            i++;
        }

        try {
            //处理
            List<String> result = new ArrayList<>();
            for (Future<List<String>> future : futures) {
                //合并操作
                result.addAll(future.get());
            }
            if (CollectionUtils.isEmpty(result)) {
                Iterator<String> iterator = result.iterator();
                while (iterator.hasNext()) {
                    String value = iterator.next();
                    System.out.println(value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private class Task implements Callable<List<String>> {

        private List<String> list;
        private int start;
        private int end;

        public Task(List<String> list, int start, int end) {
            this.list = list;
            this.start = start;
            this.end = end;
        }

        @Override
        public List<String> call() throws Exception {
            String obj = null;
            List<String> retList = new ArrayList<String>();
            for (int i = start; i < end; i++) {
                obj = list.get(i) + i;
                //你的处理逻辑
                System.out.println(obj);
                retList.add(obj);
            }
            //返回处理结果
            return retList;
        }
    }


    private void func() {
        List<String> applyHistoryList = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            applyHistoryList.add(i + "==>");
        }
        long start = System.currentTimeMillis();
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (String str : applyHistoryList) {
            Callable<String> run = new Callable<String>() {
                @Override
                public String call() throws InterruptedException {
                    str.concat("A");
                    Thread.sleep(1000);
                    if (str.equals("111")) {
                        throw new RuntimeException("");
                    }
                    System.out.println(str);
                    return str;
                }
            };

            pool.submit(run);

        }
        pool.shutdown();

        System.out.println("使用线程池一共执行：" + String.valueOf(System.currentTimeMillis() - start) + "ms");
    }
}
